package com.ffcs.crmd.tsp.server.job.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.netty.INettyInvokeCallback;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.enu.JobDetailSendType;
import com.ffcs.crmd.tsp.server.application.ApplicationManagerHolder;
import com.ffcs.crmd.tsp.server.job.info.JobDetailShell;
import com.ffcs.crmd.tsp.server.manager.JobProcessManager;
import com.ffcs.crmd.tsp.server.netty.task.TaskRegisterInfo;

import io.netty.channel.Channel;

/**
 * 
 * 功能说明:任务执行请求已被接收回调方法
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class JobSendFailCallBackService implements INettyInvokeCallback {
    
    /**
     * 日志
     */
    private final static Logger LOG               = LoggerFactory.getLogger(LoggerName.ServerLoggerName);
    
    /**
     * 通道
     */
    private Channel             channel;
    
    /**
     * 子任务运行信息
     */
    private JobDetailShell      jobDetailShell;
    
    /**
     * 子任务缓存处理类
     */
    private JobProcessManager   jobProcessManager = ApplicationManagerHolder.getBean("jobProcessManager", JobProcessManager.class);
    
    /**
     * 发送类型
     */
    private JobDetailSendType   sendJobDetailType;
    
    public JobSendFailCallBackService(JobDetailShell jobDetailShell, Channel channel, TaskRegisterInfo taskRegisterInfo,
        JobDetailSendType sendJobDetailType) {
        this.channel = channel;
        this.jobDetailShell = jobDetailShell;
        this.sendJobDetailType = sendJobDetailType;
    }
    
    @Override
    public void operationComplete(RemotingCommand requestCommand) {
        jobProcessManager.removeJobDetailRunning(channel, jobDetailShell);
        switch (sendJobDetailType) {
            case NORMAL:
                jobProcessManager.putJobDetailRetry(jobDetailShell);
                LOG.warn("SERVER-NETTY: send a job detail request fail will retry to queue jobKey=[{}], id=[{}], opaque=[{}] channel={}.",
                    jobDetailShell.getContext().getTspJobInstance().getJobKey(), jobDetailShell.getId(), jobDetailShell.getRemotingOpaque(), channel);
                break;
            case DEATH:
                jobProcessManager.putJobDetailDeath(jobDetailShell);
                LOG.warn("SERVER-NETTY: send a job detail request fail will retry to death queue jobKey=[{}], id=[{}], opaque=[{}] channel={}.",
                    jobDetailShell.getContext().getTspJobInstance().getJobKey(), jobDetailShell.getId(), jobDetailShell.getRemotingOpaque(), channel);
                break;
            case TRANSFER:
                Channel lastChannel = jobProcessManager.getJobDetailRunningLastSecondChannel(jobDetailShell);
                jobProcessManager.putJobDetailRunning(jobDetailShell, lastChannel);
                LOG.warn("SERVER-NETTY: send a job detail request fail will retry to old running queue jobKey=[{}], id=[{}], opaque=[{}] channel={}.",
                    jobDetailShell.getContext().getTspJobInstance().getJobKey(), jobDetailShell.getId(), jobDetailShell.getRemotingOpaque(), channel);
                break;
        }
        
        
    }
}
